﻿var datagrid = (function () {
    function cinit(g)
    {
        g.id = webjs.newid();
        g.name = "grid_" + g.id;
        if (g.applyTo != "")
        {
            $("#" + g.applyTo).attr("id", g.id);
            var cs = ccolumns(g);
            $("#" + g.id).append("<div id='" + g.name + "'>");
            $("#" + g.name).datagrid({
                singleSelect: true,
                cache: false,
                fit: true,
                fitColumns: false,
                border: false,
                loadMsg: "数据读取中，请稍后。。。",
                columns: [cs],
                onDblClickRow: function (rowIndex, rowData) {
                    g.datasource.selectItem = rowData;
                    g.onDblClickRow(g,rowIndex, rowData);
                },
                onSelect: function (rowIndex, rowData) {
                    g.datasource.selectItem = rowData;
                }
            });
            if (g.isremove) {
                $("#" + g.name).datagrid({
                    toolbar: [
                        {
                            text: '删除',
                            iconCls: 'icon-remove',
                            handler: function () {
                                var row = g.datasource.selectItem;
                                g.onRemove(row);
                            }
                        },
                         {
                             text: '提交',
                             iconCls: 'icon-ok',
                             handler: function () {
                                 var row = g.datasource.selectItem;
                                 g.onSubmit();
                             }
                         }
                    ]
                });
            }
            if (g.issearch)
            {
                $("#" + g.name).datagrid({
                    toolbar: [
                        {
                            text: '待作业',
                            iconCls: 'icon-search',
                            handler: function () {
                                g.searchwhere = 0;
                                g.onSearch(g.searchwhere);
                            }
                        },
                        {
                            text: '全部明细',
                            iconCls: 'icon-search',
                            handler: function () {
                                g.searchwhere = 1;
                                g.onSearch(g.searchwhere);
                            }
                        }

                    ]
                });
            }
        }
    }
    function ccolumns(grid) {
        csource(grid);
        var rows = [];
        var n = 0;
        for (var i = 0; i < grid.items.length; i++) {
            var nnn=grid.items[i].field 
            if (nnn === "ROWINDEX" || nnn === "MATERIALCODE" || nnn === "FMODEL" || nnn === 'CREATEDBY' || nnn === 'CREATIONDATE')
                continue;

            rows[n] = grid.items[i];
            var field = rows[n].field;
            rows[n].align = "center";
            var fitem = grid.datasource.getitem(field)
            if (field == "MCODE") {
                rows[n].title = "商品";
                rows[n].formatter = function (value, row, index) {
                    var item = grid.datasource.getitem("MATERIALCODE");
                    var n = row[item.foreign.displayname];
                    return value + "</br>" + n + "&nbsp;&nbsp;" + row.FMODEL;
                }
            }

            if (fitem.type == "bool") {
                rows[n].formatter = function (value, row, index) {
                    if (row.footer)
                        return value;
                    var b = false;
                    if (value === 1 || value === "1" || value === true || value === "true")
                        b = true;
                    if (b === true)
                        return "是";
                    else
                        return "否";

                }
            }
            if (fitem.type == 'date') {
                rows[n].formatter = function (value, row, index) {
                    if (row.footer)
                        return value;
                    if (value == '1900-01-01T00:00:00')
                        return "";
                    else
                        return value;
                }
            }
            var combox = grid.combox[field];
            var foreign = grid.foreign[field];
            if (combox) {
                rows[n].formatter = function (value, row, index) {
                    if (row.footer)
                        return value;
                    var tt = this.field;
                    var com = grid.combox[tt];
                    if (value !== "" && com) {
                        for (var i in com.items) {
                            if (com.items[i].Value == value)
                                return com.items[i].Text;
                        }
                    }
                }
            }
            if (foreign) {
                rows[n].formatter = function (value, row, index) {
                    if (row.footer)
                        return value;
                    var tt = this.field;
                    if (grid.foreign[tt]) {
                        var fd = grid.foreign[tt];
                        if (fd.displayname != null && fd.displayname != "") {
                            var d = row[fd.displayname];
                            return d;
                        }
                        else
                            return value;
                    }
                    else
                        return value;
                }
            }
            n = n + 1;
        }
        return rows;
    }
    function csource(g) {
        if (g.datasource) {
            g.keys = g.datasource.keys;
            g.items = g.datasource.items;
            g.modelname = g.datasource.modeldata.name;
            for (var n in g.datasource.modeldata.childitem) {
                var i = g.datasource.modeldata.childitem[n];
                if (i.comvtp != null && i.comvtp.isvtp)
                    g.combox[i.field] = i.comvtp;
                if (i.foreign != null && i.foreign.isfkey) {
                    g.foreign[i.field] = i.foreign;
                }
            }
            g.datasource.onLoad = function (data) {
                g.load(data);
            };
        }
    }
    function cpage(grid) {
        $("#" + grid.name).datagrid({ pagination: true });
        grid.pageinit = true;
        var p = $("#" + grid.name).datagrid('getPager');
        if (p) {
            $(p).pagination({
                showPageList:false,
                beforePageText: "页",
                afterPageText: "共{pages}页",
                displayMsg: "从{from}-{to}行  总{total}行",
                onSelectPage: function (page, rows) {
                    grid.pageindex = page;
                    if (grid.selectpage) {
                        grid.selectpage(page, rows);
                    }
                }
            });
        }
    }
    return {
        applyTo: "",
        keys: null,
        id: false,
        items: null,
        datasource: null,
        name: "",
        check: false,
        combox: [],
        foreign: [],
        fit: true,
        pageinit: false,
        fitcolumns: true,
        isremove: false,
        init: function () {
            cinit(this);
            this.pagination();
        },
        load: function (data) {
            if (!data || !data.rows) {
                data = {};
                data.rows = [];
                data.total = 0;
            }
            $("#" + this.name).datagrid('loadData', data);
            if (this.parent) {
                this.parent.updateData(this.tabindex, data);
            }
            this.loaded();
        },
        loading: function () {
            $("#" + this.name).datagrid('loading');
        },
        loaded: function () {
            $("#" + this.name).datagrid('loaded');
        },
        onDblClickRow: function (grid, rowIndex, rowData) {

        },
        insertRow: function (row) {
            $("#" + this.name).datagrid('appendRow', row);
        },
        pagination: function (p) {
            cpage(this);
        },
        selectpage: function (page,rows) {
            var dg = this;
            if (dg.datasource) {
                $("#" + dg.name).datagrid('loading');
                dg.datasource.selectpage(page, rows);
            }
        },
        selectRow: function (index) {
            var row = $("#" + this.name).datagrid('getSelected');
            if (row) {
                var si = $("#" + this.name).datagrid('getRowIndex', row);
                index = (si + index);
                if (index < 0) {
                    index = 0;
                }
                else {
                    var rows = $("#" + this.name).datagrid('getRows')
                    var count = rows.length - 1;
                    if (index > count)
                        index = count;
                }
                $("#" + this.name).datagrid('selectRow', index);
                this.selectindex = index;
            }
            else
                $("#" + this.name).datagrid('selectRow', 0);
        },
        nfpage: function (page)
        {
            if (this.pageindex === undefined)
                this.pageindex = 1;
            this.pageindex = (this.pageindex + page);
            if (this.pageindex < 1)
                this.pageindex = 1;
            var p = $("#" + this.name).datagrid('getPager');
            var count = p.total / p.pageSize;
            if (this.pageindex > count)
                this.pageindex = (this.pageindex - 1);
            $(p).pagination('select', this.pageindex);
        },
        buttonclick: function (index)
        {
            var opt = $("#" + this.name).datagrid('options');
            var bc = opt.toolbar[index];
            if (bc)
                bc.handler();

        }
    }
})